package com.kanesoft.android.gdx.actors.fight;

import com.badlogic.gdx.math.Vector2;
import com.kanesoft.android.gdx.actors.ActorPhysicsParameters;
import com.kanesoft.android.gdx.widgets.sprites.SpriteInfo;

public class FighterFalling extends FightActorStateMachine {
    
    FightActorObject fightActor;
    
    public FighterFalling(SpriteInfo stateSpriteInfo, FightActorObject actor) {
        super(stateSpriteInfo, actor);
        fightActor = actor;
    }

    @Override
    public boolean OnEnter() {
        
        if (fightActor.getActorState() != null) {
            if (fightActor.getActorState().equals(fightActor.fallingState)) return false;
            if (fightActor.getActorState().equals(fightActor.fallingKickState)) return false;
            if (fightActor.getActorState().equals(fightActor.fallingPunchState)) return false;
        }
        if (actor.getWorldControl().isObjectOnTheGround(actor)) return false;
        
        actorSprite.setAnimationInLoop(true);
        actorSprite.setSpriteInfo(stateSpriteInfo);

        return true;
    }

    @Override
    public boolean OnUpdate() {

        if (actor.getWorldControl().isObjectOnTheGround(actor)) {
            actor.setActorState(fightActor.waitingState);
            return false;
        }

        fightActor.getSensorFixture().setFriction(0.2f);

        Vector2 objVelocity = fightActor.getActorBody().getLinearVelocity();
        ActorPhysicsParameters objectPhysicsParams = fightActor.getPhysicsParameters();

        
        if (Math.abs(objVelocity.x) > objectPhysicsParams.bodyJumpMaxSpeed) {
            fightActor.getActorBody().setLinearVelocity(Math.signum(objVelocity.x) * objectPhysicsParams.bodyJumpMaxSpeed,
                    objVelocity.y);
        }
        
        fightActor.getSensorFixture().setFriction(0f);
        
        return true;
    }

    @Override
    public String getStateName() {
        return "Falling";
    }

}
